﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _430GroupProject;
using _430GroupProject.Messaging;
using Model.Messaging;
using System.Net.Mail;

namespace WebClient
{
    public partial class UserMessages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("~/NotAuthorized.aspx");
            }
        }

        protected void SwitchToCreateMessageButton_Click(object sender, EventArgs e)
        {
            

            this.CreateMessagePanel.Visible = true;
            this.YourMessagesPanel.Visible = false;
        }

        protected void SwitchToViewMessageButton_Click(object sender, EventArgs e)
        {
            this.CreateMessagePanel.Visible = false;
            this.YourMessagesPanel.Visible = true;

            LoadMessages();
        }

        private void LoadMessages()
        {
            User user = (User)Session["user"];
            Messages sent = new Messages();
            Messages recieved = new Messages();

            recieved = Master.Proxy.getMessageTo(user.NetID);
            sent = Master.Proxy.getMessagesFrom(user.NetID);
            List<Message> allMessages = new List<Message>();
            allMessages.AddRange(recieved.AllMessages);
            allMessages.AddRange(sent.AllMessages);

            MessagesGrid.DataSource = allMessages;
            MessagesGrid.DataBind();


            
        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            if (MessageContent.Text.Equals("") || RecipientBox.Text.Equals("") || SubjectBox.Text.Equals(""))
            {
                this.ErrorPanel.Visible = true;
                this.ErrorText.Text = "We're sorry, but you have left one of the required fields (Recipient, Message, or Subject) Blank.  Please complete your message.";
                this.MainUpdatePanel.Update();
            }
            else
            {
                User user = (User)Session["user"];
                string recList = RecipientBox.Text;
                string[] recipents = recList.Split(',');
                List<String> RecipientList = new List<string>();
                foreach (string r in recipents)
                { //Validate Recipients
                    if (Master.Proxy.getUser(r) != null)
                    {
                        RecipientList.Add(r);
                    }
                }

                Message toSend = new Message(user.NetID, RecipientList, MessageContent.Text, SubjectBox.Text);

                Master.Proxy.sendMessage(toSend);

                //Send Email
                if (!EmailBox.Text.Equals(""))
                {
                    recList = EmailBox.Text;
                    recipents = recList.Split(',');
                    RecipientList = new List<string>();
                     //Validate Recipients
                        

                        MailMessage mailObj = new MailMessage(user.Email, recList, SubjectBox.Text, MessageContent.Text);

                        SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
                        SMTPServer.Credentials = new System.Net.NetworkCredential("solshura", "lilhssweet1");
                        try
                        {
                            SMTPServer.Send(mailObj);
                        }
                        catch (Exception ex)
                        {
                            this.ErrorPanel.Visible = true;
                            this.ErrorText.Text = "Email Message was unable to be sent.";
                        }


                        this.CreateMessagePanel.Visible = false;
                        this.YourMessagesPanel.Visible = true;

                        LoadMessages();
                        this.MainUpdatePanel.Update();
                    //Response.Redirect(Request.RawUrl);
                }
            }
        }

        protected void MessagesGrid_DataBinding(object sender, EventArgs e)
        {

        }

        protected void MessagesGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //e.Row.Cells[0].Text = e.Row.DataItem.ToString();
        }

        protected void MessagesGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = MessagesGrid.Rows[index];
            if(e.CommandName=="Read"){
                row.Cells[5].Text = "Read";
            }
        }

        protected void ErrorClose_Click(object sender, EventArgs e)
        {
            ErrorPanel.Visible = false;
        }
    }
}